package com.dbh.company.buildbook.video;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class VideoUtil {

    /**
     * 调用系统命令转码每个文件
     * @param inputFile 输入视频文件路径
     * @param outputDir 输出目录路径
     */
    private static void transferToH264(File inputFile, File outputDir){
        if (!outputDir.exists()){
            outputDir.mkdirs();
        }
        String outputFile = outputDir.getPath()+"/"+inputFile.getParent();
        File outDir = new File(outputFile);
        if(!outDir.exists()){
            outDir.mkdirs();
        }
        System.out.println(outputFile+"/"+inputFile.getName());
        outputFile = outputFile+"/"+inputFile.getName();
        String command = "ffmpeg -threads 5 -i " + inputFile.getAbsolutePath() + " -vcodec libx264 -preset ultrafast -r 29 -s 1920x1080 "+outputFile;
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(command);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;

            while ( (line = br.readLine()) != null)
                System.out.println(line);

            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

}
